<?php
declare (strict_types=1);

namespace app\admin\controller;

use think\App;
use app\common\model\UserNotify;
use think\facade\Db;

class Notice extends Base
{

    public function group(): \think\response\Json
    {
        $result = [
            [
                'class' => 'layui-this',
                'title' => '订单通知',
                'count' => UserNotify::where('type', 1)
                    ->where('admin_part', 1)->whereNotLike('admin_read_ids', '%' . $this->admin->id . '%')->count(),
                'type' => 1
            ],
            [
                'class' => '',
                'title' => '注册通知',
                'count' => UserNotify::where('type', 2)->where('admin_part', 1)
                    ->whereNotLike('admin_read_ids', '%' . $this->admin->id . '%')->count(),
                'type' => 2
            ],
            [
                'class' => '',
                'title' => '建议反馈',
                'count' => UserNotify::where('type', 3)->where('admin_part', 1)
                    ->whereNotLike('admin_read_ids', '%' . $this->admin->id . '%')->count(),
                'type' => 3
            ],
        ];
        return json(['code' => 1, 'result' => $result]);
    }

    public function getData(): \think\response\Json
    {
        $param = $this->request->param();

        $map = ['is_del' => 0, 'type' => $param['type'], 'admin_part' => 1];

        $list = UserNotify::getPageList($map, '*', $param['page'], 20, false, ['user'], [],[],
            ['field' => 'admin_read_ids', 'keyword' => '%' . $this->admin->id . '%']);

        foreach ($list['data'] as $v) {
            if (!$v->admin_read_ids) {
                $v->is_admin_read = 0;
            } else {
                $kul = explode('|', $v->admin_read_ids);
                if (in_array($this->admin->id, $kul)) {
                    $v->is_admin_read = 1;
                } else {
                    $v->is_admin_read = 0;
                }
            }
        }
        return json($list);
    }

    public function all_read($type): \think\response\Json
    {

        try {
            $sql = 'UPDATE user_notify set admin_read_ids = CONCAT(admin_read_ids,"|",' . $this->admin->id . ') where type = ' . $type . ' and admin_part = 1';
            Db::query($sql);
            return json(['code' => 1, 'msg' => '已标记全部已读']);

        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }

    }

    public function noticeCount(): \think\response\Json
    {
        $count = UserNotify::whereNotLike('admin_read_ids', '%' . $this->admin->id . '%')->count();
        return json(['code' => 1, 'msg' => '', 'result' => $count]);
    }

    public function show_order($id, $source): \think\response\Redirect
    {
        $this->readCheck($id);
        return redirect(url('order/edit', ['name' => '', 'id' => $source])->build());
    }

    public function show_user($id, $source): \think\response\Redirect
    {
        $this->readCheck($id);
        return redirect(url('user/edit', ['name' => '', 'id' => $source])->build());
    }

    public function show_message($id, $source): \think\response\Redirect
    {
        $this->readCheck($id);
        return redirect(url('message/edit', ['name' => '', 'id' => $source])->build());
    }

    public function readCheck($id)
    {
        $un = UserNotify::where('id', $id)->find();
        if (!$un->admin_read_ids) {
            $un->admin_read_ids = $this->admin->id;
            $un->save();
        } else {
            $kul = explode('|', $un->admin_read_ids);
            if (!in_array($this->admin->id, $kul)) {
                $un->admin_read_ids = $un->admin_read_ids . '|' . $this->admin->id;
                $un->save();
            }
        }

    }

}
